/**
 * 定义数采仪管理模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.voc.vocQyManager.assetsRegister.assetsRegisterManager', ['water.entity.assetsRegister','water.apps.common.assetsRegister.assetsRegisterManager'
	                                                                                                ,'water.entity.monitorStation']);
	
	module.controller('vocAssetsRegisterManager',function($rootScope,$scope,$assetsRegisterService,$monitorStationService,$controller){
		/**
		 * 站点类型数据
		 */
		$scope.stationType=[{k:'all',v:'全部'},{k:'0',v:'总入口'},{k:'1',v:'总排口'}];
		/**
		 * 类型选择
		 */
		$scope.flowType="all";
		/**
		 *初始化 站点数据
		 */
		$scope.stationSelectList=[];
		/**
		 * 新建站点数据
		 */
		$scope.stationSelecter=[];
		var control = $controller("commonDataAssetsRegisterManagerController",{$scope:$scope});
		$scope.dataPramChangedBroadCast();
		/**
		 * 监听站点列表点击事件
		 */
		if($wdp.viewValue.wwtpDetailParmWatch){
			$wdp.viewValue.wwtpDetailParmWatch();
		}
		$wdp.viewValue.wwtpDetailParmWatch = $rootScope.$watch(function(){return $wdp.viewValue.wwtpDetailParm;},function(){
			if(!$wdp.viewValue.wwtpDetailParm){
				return;
			}
			if($wdp.viewValue.wwtpDetailParm.WWTPID){
			$scope._getList();
			$scope._getStationSelectList();
			}
        },true);
		
		/**
		 * 站点选择
		 */
		$scope.onFlowTypeClick=function(){
			$scope._getList();
		};
		/**
		 * 提交新建按钮点击事件
		 */
		$scope.onSubmitCreateClick = function(data){
			data.WWTP_ID=$wdp.viewValue.wwtpDetailParm.WWTPID;
			if($scope.saveVerify(data)==false){
				return;
			}
			$assetsRegisterService.createLifecycleModel(data).then(function(){//资产管理生命周期数据新建
				$assetsRegisterService.createModel(data).then(function(){
					$.messager.alert("保存成功","保存成功",'info');
					$scope.createDialog.close();
					$scope._getList();
				});
			});
		}
		/**
		 * 提交编辑按钮点击事件
		 */
		$scope.onSubmitEditClick = function(data){
			if($scope.saveVerify(data)==false){
				return;
			}
			if(data.GUID){
				$assetsRegisterService.updateLifecycleModel(data).then(function(){
					$.messager.alert("保存成功","保存成功",'info');				
					$scope.editDialog.close();
					$scope._getList();
				});
			}
			else{
				delete data.GUID;
				$assetsRegisterService.createLifecycleModel(data).then(function(){
					$.messager.alert("保存成功","保存成功",'info');				
					$scope.editDialog.close();
					$scope._getList();
				});
			}
		}
		/**
		 * 删除仪器
		 */
		$scope.onDeleteClick = function(data){
			$.messager.confirm("确认","确定要删除这条记录吗？",function(r){
				if(r){
					$assetsRegisterService.deleteLifecycleModel(data.GUID).then(function(){
						$assetsRegisterService.deleteModel(data.DEVICE_NUM).then(function(){
							$.messager.alert("删除成功","删除成功",'info');
							$scope._getList();
						});
					});
				}
			});
				
		}
		/*
		 * 获取设备资产记录
		 */
		$scope._getList=function _getList(){
			var queryObj = {};
			queryObj.WWTP_ID =$wdp.viewValue.wwtpDetailParm.WWTPID;		
			queryObj.FLOW_TYPE=$scope.flowType;
			if(!queryObj.WWTP_ID) {
				return;
			}
			if($scope.serveOptionModel!="all"){
				queryObj.IS_OOS=$scope.serveOptionModel;
			}
			if($scope.flowType=="all"){
				delete queryObj.FLOW_TYPE;
			}
			
			
			$assetsRegisterService.getList(queryObj).then(function(d){
				$scope.assetsRegisterList=d;
			});
		};
		/**
		 * 获取站点列表，编辑或者新增的时候需要选择
		 */
		$scope._getStationSelectList = function(){
			if(!$wdp.viewValue.wwtpDetailParm.WWTPID){
				return;
			}
			$monitorStationService.getList({WWTP_ID:$wdp.viewValue.wwtpDetailParm.WWTPID}).then(function(d){
				$scope.stationSelectList = [];
				for(var i=0;i<d.length;i++){
					$scope.stationSelectList.push({
						k:d[i].EXP_NO,
						v:d[i].NAME
					});
				}
				$scope.stationSelecter.setData($scope.stationSelectList);
			});
		};
  });
}(window, angular));